package com.source.baseData.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.entity.PoultryCategoryParent;
import com.source.baseData.entity.SlaughterHouse;
import com.source.baseData.service.PoultryCategoryParentService;
import com.source.labelAndBarcode.vo.StallsVo;
import com.source.utils.CommonResult;
import com.source.utils.ExcelUtil;
import com.source.utils.MyPage;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Joey
 * @since 2021-02-22
 */
@Api(value = "父级禽类管理", tags = "父级禽类管理")
@RestController
@CrossOrigin
@RequestMapping("/baseData/poultry-category-parent")
public class PoultryCategoryParentController {

    @Autowired
    private PoultryCategoryParentService poultryParentService;

    @ApiOperation(value = "添加父级禽类")
    @PostMapping("/save")
    public CommonResult<String> save(@RequestBody @Validated PoultryCategoryParent poultry) {
        if (poultryParentService.save(poultry)) {
            return new CommonResult<String>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
        }
        return new CommonResult<String>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg());
    }

    @ApiOperation("模糊查询 - 分页")
    @GetMapping("/findByKeyWord/{keyWord}/{current}/{size}")
    public CommonResult<Page> findByKeyWord(@PathVariable String keyWord, @PathVariable Integer current, @PathVariable Integer size){

        List<PoultryCategoryParent> list = poultryParentService.findByKeyWord(keyWord);
        Page page = MyPage.list2Page(current, size, list);
        return new CommonResult<Page>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "逻辑删除父级禽类")
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable(value = "id") String id) {
        if (poultryParentService.removeById(id)) {
            return new CommonResult<String>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg());
        }
        return new CommonResult<String>(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg());
    }

    @ApiOperation(value = "编辑父级禽类")
    @PutMapping("/updateById")
    public CommonResult<String> updateById(@RequestBody PoultryCategoryParent poultry){
        if(poultryParentService.updateById(poultry)){
            return new CommonResult<String>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg());
        }
        return new CommonResult<String>(ResultEnum.PARAMS_ERROR.getCode(),ResultEnum.PARAMS_ERROR.getMsg());
    }

    @ApiOperation(value = "查找所有父级")
    @GetMapping("/findAll")
    public CommonResult<List<PoultryCategoryParent>> findAll(){
        List<PoultryCategoryParent> list = poultryParentService.list();
        return new CommonResult<List<PoultryCategoryParent>>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),list);
    }

    @ApiOperation(value = "查找所有父级", notes = "分页")
    @GetMapping("/findAllOnPagination")
    public CommonResult<IPage<PoultryCategoryParent>> findAllOnPagination(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                                          @RequestParam(value = "size", defaultValue = "10") Integer size){
        Page<PoultryCategoryParent> page = new Page<PoultryCategoryParent>(current,size);
        IPage<PoultryCategoryParent> pages = poultryParentService.findAllOnPagination(page);
        return new CommonResult<IPage<PoultryCategoryParent>>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),pages);
    }


    @ApiOperation(value = "根据id查找")
    @GetMapping("/findOne/{id}")
    public CommonResult<PoultryCategoryParent> findOne(@PathVariable(value = "id") String id){
        PoultryCategoryParent poultry = poultryParentService.getById(id);
        return new CommonResult<PoultryCategoryParent>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),poultry);
    }

    @ApiOperation(value = "上传Excel，存储数据到数据库中")
    @PostMapping(value = "/uploadExcel", produces = "application/json;charset=utf-8")
    public CommonResult<String> uploadExcel(@RequestPart(value = "excelFile", required = false) MultipartFile excelFile) throws IOException {
        if(null != excelFile && !excelFile.isEmpty()){
            ExcelUtil.uploadExcel(excelFile, PoultryCategoryParent.class, poultryParentService);
            return new CommonResult<String>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg());
        }
        return new CommonResult<String>(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg());
    }

    @ApiOperation(value = "下载excel")
    @GetMapping(value = "/downloadExcel", produces = "application/vnd.ms-excel;charset=utf-8")
    public CommonResult<String> downloadExcel(HttpServletResponse response) throws IOException {
        List<PoultryCategoryParent> allVo = poultryParentService.list();
        ExcelUtil.downloadExcel(response, PoultryCategoryParent.class, allVo);
        return new CommonResult<String>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
    }

    /*@ApiOperation(value = "下载excel")
    @GetMapping(value = "/downloadExcel", produces = "application/vnd.ms-excel;charset=utf-8")
    public CommonResult<String> downloadExcel(HttpServletResponse response) throws IOException {

        ExcelUtil.downloadExcel(response, PoultryCategoryParent.class, poultryParentService);
        return new CommonResult<String>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg());
    }*/



}

